/*
 * Created on Mar 3, 2010
 */
package com.mgmaps.tools;

import java.util.Vector;

public class MiscTools {
  
  private MiscTools() {
  }

  
  /**
   * Turn a signed byte into an unsigned byte (stored as int).
   * @param b byte to convert
   * @return the converted byte/int
   */
  public static int unsigned(byte b) {
    return ((b < 0) ? (b+256) : b);
  }
  
  
  /**
   * Add all elements in b to a.
   * @param a destination vector
   * @param b vector to add
   */
  public static void addAll(Vector a, Vector b) {
    int sz = b.size();
    for (int i=0; i<sz; i++)
      a.addElement(b.elementAt(i));
  }

  
  /**
   * Add all elements in b to a.
   * @param a vector to add to
   * @param b array to add from
   */
  public static void addAll(Vector a, Object[] b) {
    for (int i=0; i<b.length; i++)
      a.addElement(b[i]);
  }

  
  /**
   * Round a double.
   * @param num double to round
   * @return rounded number
   */
  public static double round(final double num) {
    final double floor = Math.floor(num);
    if (num - floor >= 0.5) {
      return Math.ceil(num);
    } else {
      return floor;
    }
  }

  
  /**
   * Contains method.
   * @param array array to search
   * @param key key to search
   * @return true if found, false if not found
   */
  public static boolean arrayContains(String[] array, String key) {
    for (int i=0; i<array.length; i++)
      if (array[i].equals(key))
        return true;
    return false;
  }

  
  /**
   * IndexOf implementation for array.
   * @param a array to search
   * @param o object to search
   * @return position in array
   */
  public static int indexOf(final Object[] a, final Object o) {
    if (o == null)
      return -1;
    for (int i=0; i<a.length; i++)
      if (o.equals(a[i]))
        return i;
    return -1;
  }

  public static int binarySearch(final int[] array, final int target) {
    int high = array.length;
    int low = -1;
    int probe;
    while (high - low > 1) {
      probe = (high + low) / 2;
      if (array[probe] > target) {
        high = probe;
      } else {
        low = probe;
      }
    }
    if (low == -1 || array[low] != target) {
      return -1;
    } else {
      return low;
    }
  }

  public static int binarySearch(final Comparable[] array, final Comparable target) {
    int high = array.length;
    int low = -1;
    int probe;
    while (high - low > 1) {
      probe = (high + low) / 2;
      if (array[probe].compareTo(target) > 0) {
        high = probe;
      } else {
        low = probe;
      }
    }
    if (low == -1 || !array[low].equals(target)) {
      return -1;
    } else {
      return low;
    }
  }

  public static void bubbleSort(final Comparable[] array) {
    final int elements = array.length;
    int i;
    int j;
    Comparable t = null;
    for (i = 0; i < elements; i++) {
      for (j = 1; j < (elements - i); j++) {
        if (array[j - 1].compareTo(array[j]) > 0) {
          t = array[j - 1];
          array[j - 1] = array[j];
          array[j] = t;
        }
      }
    }
  }

  public static void bubbleSort(final int array[]) {
    final int elements = array.length;
    int i;
    int j;
    int t = 0;
    for (i = 0; i < elements; i++) {
      for (j = 1; j < (elements - i); j++) {
        if (array[j - 1] > array[j]) {
          t = array[j - 1];
          array[j - 1] = array[j];
          array[j] = t;
        }
      }
    }
  }

  public static void doubleBubbleSort(final int mainArray[], final int[] secondArray) {
    final int elements = mainArray.length;
    int i;
    int j;
    int t = 0;
    int t2 = 0;
    for (i = 0; i < elements; i++) {
      for (j = 1; j < (elements - i); j++) {
        if (mainArray[j - 1] > mainArray[j]) {
          t = mainArray[j - 1];
          t2 = secondArray[j - 1];

          mainArray[j - 1] = mainArray[j];
          secondArray[j - 1] = secondArray[j];

          mainArray[j] = t;
          secondArray[j] = t2;
        }
      }
    }
  }

  public static void doubleBubbleSort(final Comparable mainArray[], final Comparable[] secondArray) {
    final int elements = mainArray.length;
    int i;
    int j;
    Comparable t;
    Comparable t2;
    for (i = 0; i < elements; i++) {
      for (j = 1; j < (elements - i); j++) {
        if (mainArray[j - 1].compareTo(mainArray[j]) > 0) {
          t = mainArray[j - 1];
          t2 = secondArray[j - 1];

          mainArray[j - 1] = mainArray[j];
          secondArray[j - 1] = secondArray[j];

          mainArray[j] = t;
          secondArray[j] = t2;
        }
      }
    }
  }

}
